from flask import Flask, render_template, request

from config.MyEnv import MyEnv
from config.db import MySQLClient
import json
from flask import jsonify
from config.db_config import db_config, tenant_db_config, refact_db_config
from service.common_service import get_request_body

db_client = MySQLClient(db_config)
tenant_db_client = MySQLClient(tenant_db_config)
refact_db_client = MySQLClient(refact_db_config)

action = "message_send"

def get_channel_send_param(request_id):
    request_body = get_request_body(action, request_id)
    url = MyEnv.host + '/pub/40040901/message/channel-send'

    if not request_body:
        return jsonify({'request_body': {}, 'url': url, 'bizId': ''})

    body_dict = json.loads(request_body)
    body_dict['tenantCode'] = 'jmtest'
    if "requestId" in body_dict:
        body_dict["requestId"] = ""
    if "receivers" in body_dict and body_dict["receivers"]:
        body_dict["receivers"][0]["receiverId"] = "4230bc6e-69e6-46a9-a39e-b929a06a84e8"
        body_dict["receivers"][0]["receiverCode"] = "admin"
    request_body = json.dumps(body_dict, ensure_ascii=False, indent=2)

    return jsonify({'request_body': request_body, 'url': url, 'bizId': body_dict["bizId"],'businessGuid': body_dict["businessGuid"]})

def get_update_param(request_id):
    request_body = get_request_body(action, request_id)
    url = MyEnv.host + '/pub/40040901/message/channel-update-status'

    if not request_body:
        return jsonify({'request_body': {}, 'url': url, 'bizId': ''})

    return_body = {}

    body_dict = json.loads(request_body)
    return_body['tenantCode'] = 'jmtest'
    return_body['bizId'] = body_dict["bizId"]
    return_body['channels'] = body_dict["channels"]
    return_body['receivers'] = [
        {
            "receiverId": "4230bc6e-69e6-46a9-a39e-b929a06a84e8",
            "receiverCode": "admin"
        }
    ]
    return_body['appCode'] = body_dict["appCode"]
    return_body['clientType'] = 3

    request_body = json.dumps(return_body, ensure_ascii=False, indent=2)

    return jsonify({'request_body': request_body, 'url': url, 'bizId': body_dict["bizId"],'businessGuid': body_dict["businessGuid"]})


def get_channel_delete_param(request_id):
    request_body = get_request_body(action, request_id)
    url = MyEnv.host + '/pub/40040901/message/channel-delete'

    if not request_body:
        return jsonify({'request_body': {}, 'url': url, 'bizId': ''})

    return_body = {}
    # 修改 receivers 字段
    body_dict = json.loads(request_body)
    return_body['tenantCode'] = 'jmtest'
    return_body['bizId'] = body_dict["bizId"]
    return_body['channels'] = body_dict["channels"]
    return_body['appCode'] = body_dict["appCode"]
    return_body['clientType'] = 3

    request_body = json.dumps(return_body, ensure_ascii=False, indent=2)
    return jsonify({'request_body': request_body, 'url': url, 'bizId': body_dict["bizId"]})


def get_channel_delete_by_business_id_param(request_id):
    request_body = get_request_body(action, request_id)
    url = MyEnv.host + '/pub/40040901/message/channel-delete'

    if not request_body:
        return jsonify({'request_body': {}, 'url': url, 'bizId': ''})

    return_body = {}
    body_dict = json.loads(request_body)
    return_body['tenantCode'] = 'jmtest'
    return_body['bizId'] = body_dict["bizId"]
    return_body['businessGuid'] = body_dict["businessGuid"]
    return_body['channels'] = body_dict["channels"]
    return_body['appCode'] = body_dict["appCode"]
    return_body['clientType'] = 3

    request_body = json.dumps(return_body, ensure_ascii=False, indent=2)

    return jsonify({'request_body': request_body, 'url': url, 'bizId': body_dict["bizId"],'businessGuid': body_dict["businessGuid"]})


def get_channel_batch_delete_param(request_id):
    request_body = get_request_body(action, request_id)
    url = MyEnv.host + '/pub/40040901/message/channel-delete-batch'

    if not request_body:
        return jsonify({'request_body': {}, 'url': url, 'bizId': ''})

    return_body = {}
    body_dict = json.loads(request_body)
    return_body['tenantCode'] = 'jmtest'
    return_body['channels'] = body_dict["channels"]
    return_body['businessGuid'] = body_dict["businessGuid"]
    return_body['appCode'] = body_dict["appCode"]

    request_body = json.dumps(return_body, ensure_ascii=False, indent=2)

    return jsonify({'request_body': request_body, 'url': url, 'bizId': body_dict["bizId"],'businessGuid': body_dict["businessGuid"]})
